package com.hzdesk.media.koreabatchmatvector.logic.view;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hzdesk.media.koreabatchmatvector.R;
import com.hzdesk.media.koreabatchmatvector.logic.contract.AboutContract;
import com.hzdesk.media.koreabatchmatvector.logic.persenter.Aboutpersenter;
import com.hzdesk.media.koreabatchmatvector.utils.MyToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/7/9.
 * 杭州席媒科技有限公司  Developers  刘传华  10
 * 佛祖镇楼 bug辟易
 * TODO: 2018/7/10                                                                               关于可立批
 */

public class AboutActivity extends BaseActivity implements AboutContract.IAboutView {

    @BindView(R.id.activity_about_iv_back)
    ImageView activityAboutIvBack;
    @BindView(R.id.activity_about_iv_logo)
    ImageView activityAboutIvLogo;
    @BindView(R.id.activity_about_tv_version)
    TextView activityAboutTvVersion;
    @BindView(R.id.activity_about_ll_service)
    LinearLayout activityAboutLlService;
    @BindView(R.id.activity_about_ll_copyright)
    LinearLayout activityAboutLlCopyright;
    @BindView(R.id.activity_about_ll_new_version)
    LinearLayout activityAboutLlNewVersion;
    @BindView(R.id.activity_about_ll_version)
    LinearLayout activityAboutLlVersion;
    @BindView(R.id.activity_about_ll_about_deskmedia)
    LinearLayout activityAboutLlAboutDeskmedia;
    @BindView(R.id.activity_about_ll_bg)
    LinearLayout activityAboutLlBg;
    @BindView(R.id.activity_about_tv_tips_prompt)
    TextView activityAboutTvTipsPrompt;
    @BindView(R.id.activity_about_tv_new_version_desc)
    TextView activityAboutTvNewVersionDesc;

    private Aboutpersenter mAboutpersenter = new Aboutpersenter(this);

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about);
        ButterKnife.bind(this);
        mAboutpersenter.setListener(activityAboutLlService);
        mAboutpersenter.setListener(activityAboutLlCopyright);
        mAboutpersenter.setListener(activityAboutLlVersion);
        mAboutpersenter.setListener(activityAboutLlAboutDeskmedia);

    }

    @OnClick({R.id.activity_about_iv_back, R.id.activity_about_iv_logo, R.id.activity_about_ll_service,
            R.id.activity_about_ll_copyright, R.id.activity_about_ll_version, R.id.activity_about_ll_about_deskmedia})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.activity_about_iv_back:
                mAboutpersenter.oncliackBack();
                break;
            case R.id.activity_about_iv_logo:
                break;
            case R.id.activity_about_ll_service:
                mAboutpersenter.onclickedServiceAgreement();
                break;
            case R.id.activity_about_ll_copyright:
              //  mAboutpersenter.onclickedCopyrightInfo();
                break;
            case R.id.activity_about_ll_version:
                mAboutpersenter.onckliacdeVersionUpdate(this);
                break;
            case R.id.activity_about_ll_about_deskmedia:
                mAboutpersenter.oncklicedAboutDeskMedia();
                break;
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        final SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        int selector = mSharedPreferences.getInt("item_skin", 0); //todo 名称
        activityAboutLlBg.setBackgroundResource(updateBg[selector]);
        String version = getAppVersionName();
        activityAboutTvVersion.setText("版本号 " + version);
        String newVersionName = mSharedPreferences.getString("software_version", version);
        if (!newVersionName.equals(version)) {
            activityAboutTvTipsPrompt.setVisibility(View.VISIBLE);
            activityAboutTvNewVersionDesc.setVisibility(View.VISIBLE);
        } else {
            activityAboutTvTipsPrompt.setVisibility(View.GONE);
            activityAboutTvNewVersionDesc.setVisibility(View.GONE);
        }
    }

    @Override
    public void toFishPager() {
        finish();
    }

    @Override
    public void toServiceAgreementPager() { //服务协议
        Intent intent = new Intent(this, AgreementActivity.class);
        intent.putExtra("webview_pager", 0);
        startActivity(intent);
    }

    @Override
    public void toCopyrightInfoPager() {  //版权信息
        Intent intent = new Intent(this, AgreementActivity.class);
        intent.putExtra("webview_pager", 1);
        startActivity(intent);
    }

    @Override
    public void toAboutDeskMediaPager() { //关于席媒
        Intent intent = new Intent(this, AgreementActivity.class);
        intent.putExtra("webview_pager", 2);
        startActivity(intent);
    }

    @Override
    public void showPromptInfo() {
        activityAboutTvTipsPrompt.setVisibility(View.VISIBLE);
        activityAboutTvNewVersionDesc.setVisibility(View.VISIBLE);
        showToast("版本正在更新...");
    }

    @Override
    public void hidenPromptInfo() {
        activityAboutTvTipsPrompt.setVisibility(View.GONE);
        activityAboutTvNewVersionDesc.setVisibility(View.GONE);
        showToast("该版本是最新的");
    }

    @Override
    public void showToast(String msg) {
        MyToastUtils.ShortToaseShow(this, msg);
    }

    /**
     * 返回当前程序版本名
     */
    public String getAppVersionName() {
        String versionName = "";
        try {
            // ---get the package info---
            PackageManager pm = getPackageManager();
            PackageInfo pi = pm.getPackageInfo(getPackageName(), 0);
            versionName = pi.versionName;
            if (versionName == null || versionName.length() <= 0) {
                return "";
            }
        } catch (Exception e) {
            Log.e("VersionInfo", "Exception", e);
        }
        return versionName;
    }
}

